package com.day240701.dao;

import com.day240701.entity.Teacher;
import com.day240701.util.JDBCUtils;

import java.sql.ResultSet;

public class TeacherDao {
    /**
     * 登录
     * @param tno
     * @param password
     * @return 返回教师信息
     */
    public Teacher login(int tno, String password) {
        Teacher teacher = null;
        ResultSet rs = JDBCUtils.doQuery("select tno, tname, sex, birthday, addr from t_teacher where tno = ? and password = ?", tno, password);
        try {
            while (rs.next()) {
                teacher = new Teacher();
                teacher.setTno(tno);
                teacher.setTname(rs.getString("tname"));
                teacher.setSex(rs.getString("sex"));
                teacher.setBirthday(rs.getDate("birthday"));
                teacher.setAddr(rs.getString("addr"));
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.doClose(rs);
        }
        return teacher;
    }
    public Teacher queryMyInfo(int tno) {
        Teacher teacher = null;
        ResultSet rs  = JDBCUtils.doQuery("select tno,tname,password,sex,birtyday,addr from t_teacher where tno =? ",tno);
        try {
            while (rs.next()) {
                teacher = new Teacher();
                teacher.setTno(tno);
                teacher.setTname(rs.getString("tname"));
                teacher.setPassword(rs.getString("password"));
                teacher.setSex(rs.getString("sex"));
                teacher.setBirthday(rs.getDate("birthday"));
                teacher.setAddr(rs.getString("addr"));
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.doClose(rs);
        }
        return teacher;
    }

    /**
     * 注册新账号
     * @param teacher
     * @return 主键 tno
     */
    public int register(Teacher teacher) {
        String sql  = "insert into t_teacher(tname,password,sex,birthday,addr)values(?,?,?,?,?)";
        int tno = JDBCUtils.doAddObject(sql,teacher.getTname(),teacher.getPassword(), teacher.getSex(),teacher.getBirthday(), teacher.getAddr());
        return  tno;
    }
}
